//
//  DvdDetailsViewController.m
//  SugarPile
//
//  Created by LC on 16/2/29.
//  Copyright © 2016年 www.lanou39.蓝鸥科技. All rights reserved.
//

#import "DvdDetailsViewController.h"
#import "DvdDetalisModel.h"
#import "LCPrefixHeader.pch"
#import "DvdDetailsCollectionViewCell.h"
#import "HotDetailsModel.h"
#import "CDViewController.h"
@interface DvdDetailsViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic, retain)UICollectionView *collectionView; /**< 主页面 */
@property (nonatomic, retain)NSMutableArray *dataArr;


@end

@implementation DvdDetailsViewController
- (void)dealloc
{
    [_ID release];
    [_collectionView release];
    [_dataArr release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 数据解析
    [self dataHandle];
    // 创建Collection
    [self createCollection];
    if(_dataArr.count != 0)
    {
    }else{
        
    }
    
}
- (void)createCollection
{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.itemSize = CGSizeMake((WIDTH - 30) / 2, ((WIDTH - 30) / 2) * 3 / 2);
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    _collectionView.backgroundColor = [UIColor floralWhite];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [_collectionView registerClass:[DvdDetailsCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self.view addSubview:_collectionView];
    __block __typeof(&*self)ws = self;
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(ws.view.mas_left).with.offset(0);
        make.right.equalTo(ws.view.mas_right).with.offset(0);
        make.top.equalTo(ws.view.mas_top).with.offset(0);
        make.bottom.equalTo(ws.view.mas_bottom).with.offset(0);
    }];
    [_collectionView release];
    [layout release];

}
// 数据解析
- (void)dataHandle
{
    
    // [url stringByReplacingOccurrencesOfString:@"76264585" withString:_ID]
    [LCAFNetWorking GETWithURL:_ID  completion:^(id result) {
        NSArray *arr = result[@"data"][@"object_list"];

        self.dataArr = [DvdDetalisModel modelHanderWithArray:arr];
      
//        self.dataArr = [DvdDetalisModel modelHanderWithArray:arr];
        NSLog(@"%@", _dataArr);
        // 刷新
        [_collectionView reloadData];
    }];

}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    DvdDetailsCollectionViewCell *cell = [_collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    if (_dataArr.count != 0) {
        DvdDetalisModel *model = _dataArr[indexPath.item];
        cell.model = model;
        cell.backgroundColor = [UIColor whiteColor];
        return cell;
    }else{
        return nil;
    }
}
#pragma mark -点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    DvdDetalisModel *m = _dataArr[indexPath.item];
    CDViewController *cdVC = [[[CDViewController alloc] init] autorelease];
    
    NSString *url = @"http://203.80.144.212/napi/album/detail/?include_fields=share_links_2%2Ccovers%2Cmembers%2Cmember_count%2Cmanagers&platform_version=4.4.4&device_platform=HM%2B2A&__dtac=%257B%257D&screen_width=720&screen_height=1280&__domain=www.duitang.com&app_version=69&album_id=76646519&platform_name=Android&locale=zh&app_code=nayutas";
    [LCAFNetWorking GETWithURL:[url stringByReplacingOccurrencesOfString:@"76646519" withString:[NSString stringWithFormat:@"%@", m.selfId]]  completion:^(id result) {
        // 处理好的专辑头字典传入第二页
        cdVC.dataDic = result[@"data"];
        // 传入ID
        cdVC.ID = m.selfId;
        cdVC.cdCount = m.count;
        
        [self.navigationController pushViewController:cdVC animated:YES];
    }];

    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
